package com.project.voicememoassistant.tools;

import com.project.voicememoassistant.model.TemplateData;
import com.project.voicememoassistant.model.WxTemplate;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class WechatSendTemplate {
//    public static String appID = "wx6c24db202cb5601d";
//    public static String appsecret = "ecb2938b7c139190021c28f47ad2496f";
    public static String appID = "wx4255af1c0ede9745";
    public static String appsecret = "af483956a22e38b410f73741b2434a36";

    /***
     * 查询大姨妈的方法
     * @param openId
     */
    public static void sendMenstruationTemplate(String url,String openId){
        WxTemplate wxTemplate = new WxTemplate();
        wxTemplate.setTemplate_id("HgQWA0EtbK37T8Ge0PeJJQwqtOY96620hjs1Y19WeCc");
//        wxTemplate.setTemplate_id("oXMbeYmhjDK2sbKCYWrSEVxpVq6rz-bYU3X4BUixHDs");
        wxTemplate.setTouser(openId);
        wxTemplate.setUrl("http://47.96.87.172/#/?uid="+openId);
        wxTemplate.setTopcolor("#FF0000");
        Map<String, TemplateData> map = new HashMap<>();
        wxTemplate.setData(map);
        JSONObject json = JSONObject.fromObject(wxTemplate);//将java对象转换为json对象
        RestTemplate restTemplate = new RestTemplate();
        Object forObject = restTemplate.postForObject(url,json,Object.class);
        System.out.println(forObject);
    }

    /***
     * 提醒15分钟的事情
     * @param url token
     * @param openId from_user_name/uid
     * @param keyValue1 目标时间
     * @param keyValue2 待完成目标的内容
     * @param keyValue3 健康小贴士
     */
    public static void sendTemplate(String url,String openId,String keyValue1,String keyValue2,String keyValue3){
        WxTemplate wxTemplate = new WxTemplate();
        wxTemplate.setTemplate_id("B7LuhDstf9hIRjb_15Bw_u6tENgjupUjODobFrycC1s");
//        wxTemplate.setTemplate_id("KBGJfOVIJ0lW7uDsTbnTjlY7BdW-tXlwKk-Z-o4Usx0");
        wxTemplate.setTouser(openId);
        wxTemplate.setUrl("http://47.96.87.172/#/Daily?uid="+openId);
        wxTemplate.setTopcolor("#FF0000");
        Map<String, TemplateData> map = new HashMap<>();
        TemplateData first = new TemplateData();
        first.setColor("#868586");
        first.setValue("主人您好，未来15分钟您有以下事情待完成~");
        map.put("first", first);
//        key1
        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#173177");
        keyword1.setValue(keyValue1);
        map.put("keyword1",keyword1);
//        key2
        TemplateData keyword2 = new TemplateData();
        keyword2.setColor("#173177");
        keyword2.setValue(keyValue2);
        map.put("keyword2",keyword2);
//        key3
        TemplateData keyword3 = new TemplateData();
        keyword3.setColor("#173177");
        keyword3.setValue(keyValue3);
        map.put("keyword3",keyword3);

        wxTemplate.setData(map);
        JSONObject json = JSONObject.fromObject(wxTemplate);//将java对象转换为json对象
        RestTemplate restTemplate = new RestTemplate();
        Object forObject = restTemplate.postForObject(url,json,Object.class);
        System.out.println(forObject);
    }

    /**
     * 网页授权access_token
     */
    public static String getAccessToken() {
        RestTemplate restTemplate = new RestTemplate();
        String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?appid="+appID+"&secret="+appsecret+"&grant_type=client_credential";
        Object forObject = restTemplate.getForObject(access_token_url, Object.class);
        System.out.println(forObject);
        JSONObject jsonToken = JSONObject.fromObject(forObject);
        String token = null;
        try {
            if (StringUtils.isNotBlank(String.valueOf(jsonToken))) {
                token = jsonToken.getString("access_token");
            }
        }catch (Exception e){
            token = "";
            e.printStackTrace();
        }
        return token;
    }
}
